package org.way.web.resource.tags;


import org.way.web.resource.utils.StaticUrlProvider;
import org.way.web.resource.utils.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * Created by way on 2017/7/13.
 */
public class JsTag extends BodyTagSupport {


    /**
     * js src
     */
    private String src;

    private String charset;

    private String type;


    @Override
    public int doEndTag() throws JspException {
        String str = "<script";
        if (StringUtils.isNotEmpty(charset)) {
            str += " charset=\"" + charset + "\"";
        }
        if (StringUtils.isNotEmpty(type)) {
            str += " type=\"" + type + "\"";
        }
        String versionSplit = "?";
        if (src.contains("?")) {
            versionSplit = "&";
        }
        str += " src='" + src  + versionSplit + "v=" + StaticUrlProvider.getVersion();
        str += "'></script>";
        writeToPage(str);
        return super.doEndTag();
    }

    /**
     * 将内容写到页面上
     *
     * @param content
     */
    private void writeToPage(String content) {
        try {
            pageContext.getOut().write(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
